<?php

namespace App\Controller\Admin;

use App\Controller\Example\AssetExample;
use App\Controller\Example\BarsExample;
use App\Controller\Example\BaseExample;
use App\Models\AdminModel;
use App\Models\AssetModel;
use App\Models\BarsModel;
use App\Models\Conn;
use Core\Common\Common;
use Core\Enums\ErrorEnum;
use Core\Result\Result;

class Bars
{
    /**
     * 管理后台店铺列表
     * entry token,id,name,startTime,endTime,page,limit
     * return json
     * */
    public function barsList($data){
        $assemble = Common::assemble(new BarsExample(), $data);
        $identical = Common::identical($data);
        $conn = new Conn();
        $joinSql = " WHERE `status` >0 ";
        if($assemble->id !=null){ $joinSql.=" and `id` ='$assemble->id' ";}
        if($assemble->name !=null){ $joinSql.=" and `name` ='$assemble->name' ";}
        if($assemble->status !=null){ $joinSql.=" and `status` ='$assemble->status' ";}
        $userSql = "SELECT `id`, `name`, `phone`,`addr`, `city`, `head_img`,`time`,`lng`,`lat`,`collectioncode`,`status` FROM `bars` $joinSql order by id desc limit $identical->pageStart,$identical->limit";
        $readConn = $conn->readConn($userSql,true);
        $returnData["list"]=$readConn;
        $userCountSql="SELECT count(id) as count FROM `bars` $joinSql";
        $returnData["count"] = $conn->readConn($userCountSql)["count"];
        $returnData["total"] = null;
        Result::success($returnData);
    }
    /**
     * 管理后台编辑店铺
     * entry token,name,phone,addr,head_img,lng,lat,collectioncode
     * return json
     * */
    public function editBars($data){
        $assemble = Common::assemble(new BarsExample(), $data);
        $conn = new Conn();
        $adminInToken = AdminModel::getAdminInToken($conn, $assemble->token);
        $BarsExample = Common::assemble(new BarsExample(), BarsModel::barsClassList($conn, $assemble));
        if($BarsExample->status != 1){
            Result::error(ErrorEnum::PROHIBITMODIFICATION);
        }
        $conn->shiwuStart();
        if(BarsModel::upBarsInside($conn,$assemble) !=1){
            $conn->shiwuRollback();
            Result::error(ErrorEnum::SUBMITFAILED);
        }
        $conn->shiwuEnd();
        AdminLog::addAdminLog($adminInToken["username"],"店铺列表","编辑店铺:[".$assemble->id."]");
        Result::success("ok");
    }
    /**
     * 添加店铺
     * entry name,phone,addr,head_img,lng,lat,collectioncode
     * return json
    */
    public function addBars($data){
        $assemble = Common::assemble(new BarsExample(), $data);
        if($assemble->name == null || $assemble->phone==null || $assemble->head_img==null || $assemble->lng==null || $assemble->lat==null || $assemble->addr==null){
            Result::error(ErrorEnum::INCOMPLETEPARAMETERS);
        }
        $conn = new Conn();
        $adminInToken = AdminModel::getAdminInToken($conn, $assemble->token);
        if(empty($adminInToken)){
            Result::error(ErrorEnum::ILLEGALOPERATION);
        }
        if($assemble->collectioncode == null){
            $assemble->collectioncode = 1;
        }
        $conn->shiwuStart();
        if(BarsModel::addBars($conn,$assemble) != 1){
            $conn->shiwuRollback();
            Result::error(ErrorEnum::ADDFAILED);
        }
        $conn->shiwuEnd();
        AdminLog::addAdminLog($adminInToken["username"],"店铺列表","添加店铺:[".$assemble->name."]");
        Result::success("ok");
    }
}